﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace COC_helper
{
    public partial class Achievement : Form
    {
        public Achievement()
        {
            InitializeComponent();
        }
        private void RefreshListBox()
        {
            try
            {
                if (Global_Value.zmb_Setting.Planing_acheves == null)
                    throw new Exception("没有成就");

                ListBox_main.Items.Clear();
                ListBox_Info.Items.Clear();
                foreach (var item in Global_Value.zmb_Setting.Planing_acheves)
                {
                    ListBox_main.Items.Add(item.Name);
                    ListBox_Info.Items.Add(item.Progress
                        + " "
                        + item.Description
                        + ""
                        + item.Conn);
                }
            }
            catch (Exception err)
            {
                Global_Value.MyLogHandle.LogPush("成就编辑器:"+ err.Message.ToString(), "Wran");
                MessageBox.Show(err.Message.ToString());
            }
        }
        private void Achievement_Shown(object sender, EventArgs e)
        {
            RefreshListBox();

        }

        private void Button_Addmain_Click(object sender, EventArgs e)
        {
            try
            {
                if (Global_Value.zmb_Setting.Planing_acheves == null)
                    Global_Value.zmb_Setting.Planing_acheves = new List<Acheve> { };
                if (TextBox_Name.Text == "")
                    throw new Exception("成就不能没有名字");
                if (TextBox_Con.Text == "")
                    throw new Exception("成就不能没有达成条件");
                if (TextBox_Pro.Text == "")
                    throw new Exception("成就不能没有达成进度");
                Acheve acheve = new Acheve();
                acheve.Name = TextBox_Name.Text;
                acheve.Date = TextBox_Time.Text;
                acheve.Conn = TextBox_Con.Text;
                acheve.Description = TextBox_Info.Text;
                acheve.Progress = TextBox_Pro.Text;
                Global_Value.zmb_Setting.Planing_acheves.Add(acheve);

                RefreshListBox();



            }
            catch (Exception err)
            {
                Global_Value.MyLogHandle.LogPush("成就编辑器:" + err.Message.ToString(), "Wran");
                //throw;
            }
            

        }

        private void ListBox_main_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ListBox_main.SelectedItem == null) return;
            Acheve acheve = Global_Value.zmb_Setting.Planing_acheves.Find(one => one.Name == ListBox_main.SelectedItem.ToString());
            if (acheve == null) return;
            TextBox_Name.Text = acheve.Name;
            TextBox_Info.Text = acheve.Description;
            TextBox_Con.Text = acheve.Conn;
            TextBox_Pro.Text = acheve.Progress;
            TextBox_Time.Text = acheve.Date;
            ListBox_Info.SelectedIndex = ListBox_main.SelectedIndex;
        }

        private void Button_InFin_Click(object sender, EventArgs e)
        {
            Global_Value.CallBackLog =
                 "成就揭示"
                + "\r\n" + TextBox_Name.Text
                + "\r\n" + TextBox_Pro.Text
                + "\r\n" + TextBox_Con.Text
                + "\r\n" + TextBox_Time.Text
                + "\r\n" + TextBox_Info.Text;



        }

        private void Button_Out_Click(object sender, EventArgs e)
        {
            Global_Value.MyLogHandle.LogPush("成就编辑器:准备删除元素 Planing_acheves", "Info");
            if (ListBox_main.SelectedItem == null) return;
            Acheve acheve = Global_Value.zmb_Setting.Planing_acheves.Find(one => one.Name == ListBox_main.SelectedItem.ToString());
            Global_Value.zmb_Setting.Planing_acheves.Remove(acheve);
            RefreshListBox();
        }

        private void label5_Click(object sender, EventArgs e)
        {
            TextBox_Time.Text = DateAndTime.DateString +'-'+ DateAndTime.TimeString;
        }
    }
}
